package cn.stylefeng.guns.modular.rest.hxStoragePut;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicGoods;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicPositionGoods;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicGoodsService;
import cn.stylefeng.guns.modular.hx.basic.service.HxBasicPositionGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.modular.hx.storage.entity.HxStoragePutDetailed;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStoragePutResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStoragePutDetailedService;
import cn.stylefeng.guns.modular.hx.storage.service.HxStoragePutService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 零售  退货处理
 * @author zcx
 */
@Controller
@RequestMapping("/rest/HxStorageReturn")
public class restHxStorageReturnController extends BaseController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private HxStoragePutService hxStoragePutService;

    @Autowired
    private HxStoragePutDetailedService hxStoragePutDetailedService;
    @Autowired
    private HxBasicGoodsService hxBasicGoodsService;
    @Autowired
    private HxBasicPositionGoodsService hxBasicPositionGoodsService;


    private Lock lock = new ReentrantLock();

    /**
     * 退货单列表
     * 查询退货单列表
     */
    @ResponseBody
    @RequestMapping("/storagelist")
    public ResponseData storagelist(HxStoragePutParam hxStoragePutParam) {
		User user = userAop.getUser();
		hxStoragePutParam.setStorageState("未完成");
		hxStoragePutParam.setDeptId(user.getDeptId());
		hxStoragePutParam.setOperateId(user.getOperateId());
		List<HxStoragePutResult> list = hxStoragePutService.restfindstoragereturnlist(hxStoragePutParam);
        return ResponseData.success(list);
    }

    /**
     * 质检入库
     */
    @ResponseBody
    @RequestMapping("/return_put")
    public ResponseData return_put(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
		User user = userAop.getUser();
		ResponseData data = new ResponseData();
		data.setSuccess(true);
        lock.lock();//获取锁
        try {
    		HxStoragePutDetailed detailed = hxStoragePutDetailedService.getById(hxStoragePutDetailedParam.getId());//入库明细
            HxBasicGoods byId = hxBasicGoodsService.getById(detailed.getGoodsId());
            BigDecimal nums = new BigDecimal(hxStoragePutDetailedParam.getRealNum());
            if(detailed.getPutState().equals("待质检")) {
                if (nums.compareTo(BigDecimal.ZERO) > 0) {
                    if (hxStoragePutDetailedParam.getApproveUnit().equals(byId.getCgUnit())) {
                        BigDecimal cgRates = new BigDecimal(byId.getCgRate());
                        BigDecimal divide = nums.multiply(cgRates).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        hxStoragePutDetailedParam.setJibenRealNum(divide.toString());
                    } else if (hxStoragePutDetailedParam.getApproveUnit().equals(byId.getKcUnit())) {
                        BigDecimal kcRates = new BigDecimal(byId.getKcRate());
                        BigDecimal divide = nums.multiply(kcRates).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        hxStoragePutDetailedParam.setJibenRealNum(divide.toString());
                    } else if (hxStoragePutDetailedParam.getApproveUnit().equals(byId.getPfUnit())) {
                        BigDecimal pfRates = new BigDecimal(byId.getPfRate());
                        BigDecimal divide = nums.multiply(pfRates).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        hxStoragePutDetailedParam.setJibenRealNum(divide.toString());
                    } else if (hxStoragePutDetailedParam.getApproveUnit().equals(byId.getPsUnit())) {
                        BigDecimal psRates = new BigDecimal(byId.getPsRate());
                        BigDecimal divide = nums.multiply(psRates).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        hxStoragePutDetailedParam.setJibenRealNum(divide.toString());
                    } else if (hxStoragePutDetailedParam.getApproveUnit().equals(byId.getJibenUnit())) {
                        hxStoragePutDetailedParam.setJibenRealNum(nums.toString());
                    } else {
                        return ResponseData.error("入库单位不正确");
                    }
                }
                if (nums.compareTo(BigDecimal.ZERO) == 0){
                    hxStoragePutDetailedParam.setJibenRealNum("0");

                }
                data = hxStoragePutDetailedService.return_put(user,detailed,hxStoragePutDetailedParam);
    		} 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
		return data;
    }

// 查询商品的全部单位
    @ResponseBody
    @RequestMapping("/detailedGoodsId")
    public ResponseData detailedGoodsId(HxStoragePutDetailedParam  hxStoragePutDetailedParam) {

        List<HxBasicGoods> list = hxBasicGoodsService.lambdaQuery()
                .select(HxBasicGoods::getJibenUnit, HxBasicGoods::getCgUnit, HxBasicGoods::getPsUnit, HxBasicGoods::getPfUnit, HxBasicGoods::getKcUnit)
                .eq(HxBasicGoods::getId, hxStoragePutDetailedParam.getGoodsId())
                .list();

        return ResponseData.success(list);
    }
    @ResponseBody
    @RequestMapping("/goodsidPosition")
    public ResponseData goodsidPosition(HxStoragePutDetailedParam  hxStoragePutDetailedParam) {

        List<HxBasicPositionGoods> list = hxBasicPositionGoodsService.lambdaQuery()
                .select(HxBasicPositionGoods::getPositionNumber)
                .eq(HxBasicPositionGoods::getGoodsId, hxStoragePutDetailedParam.getGoodsId())
                .list();

        return ResponseData.success(list);
    }
}
